package SeparateAnalysis;

/**
 * Created by IntelliJ IDEA.
 * User: Mike
 * Date: 11/12/11
 * Time: 3:39 PM
 * To change this template use File | Settings | File Templates.
 */


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


public class ChartTime extends ApplicationFrame {
    JFreeChart chart;
    private TimeSeriesCollection datasetL;
    private TimeSeriesCollection datasetR;
    private String chartTitle = "Column Temperature  & abv";
    private String chartXaxis = "Date";
    private String chartYaxisL = "Column Temperature [C]";
    private String chartYaxisR = "Vapor abv [%]";

    public JFreeChart getChart() {
        return chart;
    }

    public void setChartTitle(String chartTitle) {
        this.chartTitle = chartTitle;
    }

    public void setChartXaxis(String chartXaxis) {
        this.chartXaxis = chartXaxis;
    }

    public void setChartYaxisL(String chartYaxisL) {
        this.chartYaxisL = chartYaxisL;
    }

    public void setChartYaxisR(String chartYaxisR) {
        this.chartYaxisR = chartYaxisR;
    }



    public ChartTime(final String title) {
        super(title);

        this.chart = ChartFactory.createTimeSeriesChart(
                chartTitle,
                chartXaxis,
                chartYaxisL,
                createDataset1(),
                true,
                true,
                false
        );
        final XYPlot plot = chart.getXYPlot();
        chart.setBackgroundPaint(Color.white);
        chart.setBorderPaint(Color.white);
        final NumberAxis axis2 = new NumberAxis(chartYaxisR);
        axis2.setAutoRangeIncludesZero(false);
        plot.setRangeAxis(1, axis2);
        plot.setBackgroundPaint(Color.white);
        plot.setDataset(1, createDataset2());
        plot.mapDatasetToRangeAxis(1, 1);

        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());

        renderer.setSeriesPaint(0, Color.blue);

        if (renderer instanceof StandardXYItemRenderer) {
            final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
//            rr.setPlotShapes(true);
            rr.setShapesFilled(true);
            rr.setBaseShapesVisible(true);
        }

        final StandardXYItemRenderer render1 = new StandardXYItemRenderer();
        render1.setSeriesPaint(0, Color.blue);
//        renderer2.setPlotShapes(true);
        render1.setShapesFilled(true);
        render1.setBaseShapesVisible(true);
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(0, render1);

        final StandardXYItemRenderer render2 = new StandardXYItemRenderer();
        render2.setSeriesPaint(0, Color.black);
//        renderer2.setPlotShapes(true);
        render2.setShapesFilled(true);
        render2.setBaseShapesVisible(true);
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(1, render2);

        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);

    }


    public void CreateChartTime() {
        chart = ChartFactory.createTimeSeriesChart(
                chartTitle,
                chartXaxis,
                chartYaxisL,
                datasetL,
                true,
                true,
                false
        );
        final XYPlot plot = chart.getXYPlot();
        chart.setBackgroundPaint(Color.white);
        chart.setBorderPaint(Color.white);
        final NumberAxis axis2 = new NumberAxis(chartYaxisR);
        axis2.setAutoRangeIncludesZero(false);
        plot.setRangeAxis(1, axis2);
        plot.setBackgroundPaint(Color.white);
        plot.setDataset(1, datasetR);
        plot.mapDatasetToRangeAxis(1, 1);

        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());

        renderer.setSeriesPaint(0, Color.blue);

        if (renderer instanceof StandardXYItemRenderer) {
            final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setShapesFilled(true);
            rr.setBaseShapesVisible(true);
        }

        final StandardXYItemRenderer render1 = new StandardXYItemRenderer();
        render1.setSeriesPaint(0, Color.blue);
        render1.setShapesFilled(true);
        render1.setBaseShapesVisible(true);
        render1.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(0, render1);
        final StandardXYItemRenderer render2 = new StandardXYItemRenderer();
        render2.setSeriesPaint(0, Color.black);
        render2.setShapesFilled(true);
        render2.setBaseShapesVisible(true);
        render2.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(1, render2);
        final DateAxis axis = (DateAxis) plot.getDomainAxis();
//        axis.setDateFormatOverride(new SimpleDateFormat("DD-MMM-yyyy"));
        axis.setDateFormatOverride(new SimpleDateFormat("hh:mm a EEE, MMM d"));
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    public void updateChart(ArrayList<Double> milliT, ArrayList<Double> columnC, ArrayList<Double> abv) {
        createDatasetL(milliT, columnC);
        createDatasetR(milliT, abv);
        CreateChartTime();
    }

    public void createDatasetL(ArrayList<Double> milliT, ArrayList<Double> Value) {
        final TimeSeries s1 = new TimeSeries("Column Temp", Second.class);
        for (int i = 0; i < milliT.size(); i++) {
            Double dtemp = milliT.get(i);
            Long longs = dtemp.longValue();
            Date rDate = new Date(longs);
            s1.add(new Second(new Date(longs)), Value.get(i));
        }
        datasetL = new TimeSeriesCollection();
        datasetL.addSeries(s1);
    }

    public void createDatasetR(ArrayList<Double> milliT, ArrayList<Double> Value) {
        final TimeSeries s1 = new TimeSeries("Vapor abv", Second.class);
        for (int i = 0; i < milliT.size(); i++) {

            Double dtemp = milliT.get(i);
            Long longs = dtemp.longValue();

            Date rDate = new Date(longs);
            s1.add(new Second(new Date(longs)), Value.get(i));
        }
        datasetR = new TimeSeriesCollection();
        datasetR.addSeries(s1);
    }


    public XYDataset createDataset1() {
        final TimeSeries s1 = new TimeSeries("Random Data 1", Minute.class);
        s1.add(new Minute(1,1,1,2,2001), 181.8);
        s1.add(new Minute(3, 1,1,2,2001), 167.3);
        s1.add(new Minute(4, 1,1,2,2001), 153.8);
        s1.add(new Minute(5, 1,1,2,2001), 167.6);
        s1.add(new Minute(6, 1,1,2,2001), 158.8);
        s1.add(new Minute(7, 1,1,2,2001), 148.3);
        s1.add(new Minute(8, 1,1,2,2001), 153.9);
        s1.add(new Minute(9, 1,1,2,2001), 142.7);
        s1.add(new Minute(10, 1,1,2,2001), 123.2);
        s1.add(new Minute(11, 1,1,2,2001), 131.8);
        s1.add(new Minute(12, 1,1,2,2001), 139.6);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        return dataset;
    }


    public XYDataset createDataset2() {
        final TimeSeries s2 = new TimeSeries("Random Data 2", Minute.class);
//        final TimeSeries s1 = new TimeSeries("Random Data 2", Millisecond.class);
        s2.add(new Minute(2, 1,1,2,2001), 429.6);
        s2.add(new Minute(3, 1,1,2,2001), 323.2);
        s2.add(new Minute(4, 1,1,2,2001), 417.2);
        s2.add(new Minute(5, 1,1,2,2001), 624.1);
        s2.add(new Minute(6, 1,1,2,2001), 422.6);
        s2.add(new Minute(7, 1,1,2,2001), 619.2);
        s2.add(new Minute(8, 1,1,2,2001), 416.5);
        s2.add(new Minute(9, 1,1,2,2001), 512.7);
        s2.add(new Minute(10, 1,1,2,2001), 501.5);
        s2.add(new Minute(11, 1,1,2,2001), 306.1);
        s2.add(new Minute(12, 1,1,2,2001), 410.3);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s2);
        return dataset;
    }

    public static void main(final String[] args) {
        final ChartTime demo = new ChartTime("Dual Axis Demo 2");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}